package com.zero.manage.web.components.controller;

import com.zero.manage.web.utils.FileUtil;
import io.gitee.zerowsh.util.PublicResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
@Api(tags = "文件上传")
public class FileController {

    /**
     * 实现单个文件上传
     */
    @PostMapping(value = "upload")
    @ApiOperation(value = "上传文件")
    public PublicResult upload(@RequestParam MultipartFile file) {
        return PublicResult.ok(FileUtil.uploadFile(file));
    }

    /**
     * 文件下载
     *
     * @param res
     * @param fileName 要下载的文件名
     */
    @GetMapping("download")
    @ApiOperation(value = "下载文件")
    public void download(HttpServletResponse res, @RequestParam String fileName) {
        FileUtil.download(res, fileName);
    }


    /**
     * 文件删除
     *
     * @param fileName 要删除的文件名
     * @return
     */
    @GetMapping("del")
    @ApiOperation(value = "删除文件")
    public PublicResult del(@RequestParam String fileName) {
        FileUtil.delFile(fileName);
        return PublicResult.ok();
    }
}
